package com.htgd.gardenexpo.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.htgd.gardenexpo.dao.DelayMessageMapper;
import com.htgd.gardenexpo.entity.DelayMessage;
import com.htgd.gardenexpo.entity.GeConCompetitionSports;
import com.htgd.gardenexpo.entity.GeConLeisureSports;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.ZoneOffset;
import java.util.List;
import java.util.Objects;

/**
 * @author: 王秀田
 * @date: 2024/12/5 13:30
 * @description: 项目启动时读取消息消费表数据将放到redis队列中
 */
@Component
public class DatabaseInitializer implements CommandLineRunner {

    @Autowired
    private DelayMessageMapper delayMessageMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    private static final String DELAYED_MESSAGES_ZSET = "delayed:messages";

    @Override
    public void run(String... args) {
        List<DelayMessage> delayMessageList = delayMessageMapper.selectList(new LambdaQueryWrapper<>());
        if(!delayMessageList.isEmpty()){
            delayMessageList.stream().forEach(item->{
                if(Objects.equals(item.getType(),"1")){
                    GeConCompetitionSports conCompetitionSports = JSONObject.parseObject(item.getContent(),
                            GeConCompetitionSports.class);
                    if(Objects.equals(item.getStatus(),"1")){
                        redisTemplate.opsForZSet().add(DELAYED_MESSAGES_ZSET, JSON.toJSONString(item),
                                conCompetitionSports.getStartTime().toEpochSecond(ZoneOffset.of("+8")) * 1000);
                    }
                    if(Objects.equals(item.getStatus(),"2")){
                        redisTemplate.opsForZSet().add(DELAYED_MESSAGES_ZSET, JSON.toJSONString(item),
                                conCompetitionSports.getEndTime().toEpochSecond(ZoneOffset.of("+8")) * 1000);
                    }
                }
                if(Objects.equals(item.getType(),"2")){
                    GeConLeisureSports geConLeisureSports = JSONObject.parseObject(item.getContent(),
                            GeConLeisureSports.class);
                    if(Objects.equals(item.getStatus(),"1")){
                        redisTemplate.opsForZSet().add(DELAYED_MESSAGES_ZSET, JSON.toJSONString(item),
                                geConLeisureSports.getStartTime().toEpochSecond(ZoneOffset.of("+8")) * 1000);
                    }
                    if(Objects.equals(item.getStatus(),"2")){
                        redisTemplate.opsForZSet().add(DELAYED_MESSAGES_ZSET, JSON.toJSONString(item),
                                geConLeisureSports.getEndTime().toEpochSecond(ZoneOffset.of("+8")) * 1000);
                    }
                }
            });
        }
    }

}
